<?php
        error_reporting(E_ALL);
        ini_set('display_errors', '1');
        include_once "../inc/docroot.php";
        include_once PATH."inc/class.autores.inc.php";
        require PATH."inc/google-api-php-client-master/autoload.php";
        $autores = new autores($db);

        
if(isset($_GET['mailid']))$_SESSION['mailid'] = $_GET['mailid'];
if(isset($_GET['name']))$_SESSION['name'] = $_GET['name'];
if(isset($_GET['type']))$_SESSION['type'] = $_GET['type'];
if(isset($_GET['campaign']))$_SESSION['campaign'] = $_GET['campaign'];
if(isset($_GET['edit']))$_SESSION['edit'] = $_GET['edit'];
if(isset($_GET['add']))$_SESSION['add'] = $_GET['add'];

$ret = "";

if($_SESSION['type'] == 'submaster' && isset($_SESSION['add']) && !isset($_SESSION['access_token'])){
    $ret = $autores->isPossibleAddSubmaster($_SESSION['mailid']);
}
else if($_SESSION['type'] == 'alias' && isset($_SESSION['add']) && !isset($_SESSION['access_token'])){
    $ret = $autores->isPossibleAddAlias($_SESSION['campaign'], $_SESSION['mailid']);
}

if($ret != ""){
    echo "<div align=\"center\">";
    echo "<br/><br/>" . $ret . "<br/><br/>";
    echo "</div>";
}
else {
    $client_info = $autores->getClientInfo();

    $client_id = $client_info['client_id']; //'1097530924560-9dv7lmvrmlfsdrn3rbd8a197ci34n4og.apps.googleusercontent.com';
    $client_secret = $client_info['client_secret']; //'VMTuLVpXMyjbdi_EoyeJxUnr';
    $redirect_uri = $client_info['redirect_uri']; //'http://www.bytefact.com/mesbagapi.php';


    $client = new Google_Client();
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    if(isset($_GET['mailid']))$client->setLoginHint($_GET['mailid']);

    $client->addScope("https://mail.google.com");
    $client->setApprovalPrompt('force');
    $client->setAccessType('offline');


    $service = new Google_Service_Gmail($client);


    if (isset($_REQUEST['logout'])) {
        unset($_SESSION['access_token']);
    }



    if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
    }

    if (isset($_SESSION['access_token']) && $client->getAccessToken()) { // extract token from session and configure client
        $access_token = $client->getAccessToken();
        $profile = $service->users->getProfile('me');
        
        echo "<div align=\"center\">";
        echo "<label>Name: ". $_SESSION['name'] . "</label><br/>";
        echo "<label>Email: ". $profile->emailAddress . "</label><br/><br/><br/>";
        
        if($_SESSION['type'] == 'submaster'){
            if(isset($_SESSION['add'])){
                echo "<br/>";
                $autores->addsubmaster($profile->emailAddress, $_SESSION['access_token'], $_SESSION['name']);
                echo "<br/>";
                $autores->editalias($profile->emailAddress, $_SESSION['access_token'], $_SESSION['name']);
            }
            else if(isset($_SESSION['edit'])) {
                echo "<br/>";
                $autores->editsubmaster($profile->emailAddress, $_SESSION['access_token'], $_SESSION['name']);
                echo "<br/>";
                $autores->editalias($profile->emailAddress, $_SESSION['access_token'], $_SESSION['name']);
            }
            echo "<br/><label>Please reload Submaster Mail Window</label><br/>";
        }
        else if($_SESSION['type'] == 'alias') {
            if(isset($_SESSION['add'])){
                echo "<br/>";
                $autores->addalias($_SESSION['campaign'], $profile->emailAddress, $_SESSION['access_token'], $_SESSION['name']);
                echo "<br/>";
                $autores->editsubmaster($profile->emailAddress, $_SESSION['access_token'], $_SESSION['name']);
            }
            else if(isset($_SESSION['edit'])) {
                echo "<br/>";
                $autores->editalias($profile->emailAddress, $_SESSION['access_token'], $_SESSION['name']);
                echo "<br/>";
                $autores->editsubmaster($profile->emailAddress, $_SESSION['access_token'], $_SESSION['name']);
            }
            echo "<br/><label>Please reload Sender Mail Window</label><br/>";
        }
        
        echo "</div>";

        if(isset($_SESSION['access_token']))unset($_SESSION['access_token']);
        if(isset($_SESSION['mailid']))unset($_SESSION['mailid']);
        if(isset($_SESSION['name']))unset($_SESSION['name']);
        if(isset($_GET['code']))unset($_GET['code']);
        if(isset($_SESSION['type']))unset($_SESSION['type']);
        if(isset($_SESSION['campaign']))unset($_SESSION['campaign']);
        if(isset($_SESSION['edit']))unset($_SESSION['edit']);
        if(isset($_SESSION['add']))unset($_SESSION['add']);
        

    } else {
        $loginUrl = $client->createAuthUrl();
        //echo 'Click <a href="' . $loginUrl . '">here</a> to login';
        header('Location: ' . filter_var($loginUrl, FILTER_VALIDATE_URL));
    }
}
?>
